#include "../GeneralLibrary/Linkage.h"

ENTRY(kernelThreadFunc)
        popq	%r15
        popq	%r14
        popq	%r13
        popq	%r12
        popq	%r11
        popq	%r10
        popq	%r9
        popq	%r8
        popq	%rbx
        popq	%rcx
        popq	%rdx
        popq	%rsi
        popq	%rdi
        popq	%rbp
        popq	%rax
        movq	%rax,	%ds
        popq	%rax
        movq	%rax,	%es
        addq	$0x38,	%rsp
        movq	%rdx,	%rdi
        callq	*%rbx
        movq	%rax,	%rdi
        callq	doExit

// 这个函数要看 Ptrace.h 里面的 PerformSiteStruct，就能够看懂了，0x38是func到ss的长度 7*8=56=0x38
ENTRY(systemCall)
	sti
	subq	$0x38,	%rsp
	cld

	pushq	%rax
	movq	%es,	%rax
	pushq	%rax
	movq	%ds,	%rax
	pushq	%rax
	xorq	%rax,	%rax
	pushq	%rbp
	pushq	%rdi
	pushq	%rsi
	pushq	%rdx
	pushq	%rcx
	pushq	%rbx
	pushq	%r8
	pushq	%r9
	pushq	%r10
	pushq	%r11
	pushq	%r12
	pushq	%r13
	pushq	%r14
	pushq	%r15

	movq	$0x10,	%rdx
	movq	%rdx,	%ds
	movq	%rdx,	%es

	movq	%rsp,	%rdi            // 上面把 PerformSiteStruct 结构体构造好了，这里就把它当做参数
	callq	systemCallFunction

// returnSystemCall 必须在 systemCall 的下面，这样就可以直接执行代码了
ENTRY(returnSystemCall)
	movq	%rax,	0x80(%rsp)
	popq	%r15
	popq	%r14
	popq	%r13
	popq	%r12
	popq	%r11
	popq	%r10
	popq	%r9
	popq	%r8
	popq	%rbx
	popq	%rcx
	popq	%rdx
	popq	%rsi
	popq	%rdi
	popq	%rbp
	popq	%rax
	movq	%rax,	%ds
	popq	%rax
	movq	%rax,	%es
	popq	%rax
	addq	$0x38,	%rsp
	sti
	hlt
#	.byte	0x48
#	sysexit